package com.cqcis.uip.base.framework.common;

import com.cqcis.uip.base.framework.UIPServiceException;

/**
 * 状态行
 * 
 * @author huangmy
 * @date   2008-12-10
 */
public class StatusLine {
    private final String statusLine;
    private final String httpVersion;
    private final int statusCode;
    private final String reasonPhrase;

    public StatusLine(final String statusLine) {

        int length = statusLine.length();
        int at = 0;
        int start = 0;
        try {
            while (Character.isWhitespace(statusLine.charAt(at))) {
                ++at;
                ++start;
            }
            if (!"HTTP".equals(statusLine.substring(at, at += 4))) {
                throw new UIPServiceException("Status-Line '" + statusLine 
                    + "' does not start with HTTP");
            }
            //handle the HTTP-Version
            at = statusLine.indexOf(" ", at);
            if (at <= 0) {
                throw new UIPServiceException(
                        "Unable to parse HTTP-Version from the status line: '"
                        + statusLine + "'");
            }
            this.httpVersion = (statusLine.substring(start, at)).toUpperCase();

            //advance through spaces
            while (statusLine.charAt(at) == ' ') {
                at++;
            }

            //handle the Status-Code
            int to = statusLine.indexOf(" ", at);
            if (to < 0) {
                to = length;
            }
            try {
                this.statusCode = Integer.parseInt(statusLine.substring(at, to));
            } catch (NumberFormatException e) {
                throw new UIPServiceException(
                    "Unable to parse status code from status line: '" 
                    + statusLine + "'");
            }
            //handle the Reason-Phrase
            at = to + 1;
            if (at < length) {
                this.reasonPhrase = statusLine.substring(at).trim();
            } else {
                this.reasonPhrase = "";
            }
        } catch (StringIndexOutOfBoundsException e) {
            throw new UIPServiceException("Status-Line '" + statusLine + "' is not valid"); 
        }
        //save the original Status-Line
        this.statusLine = statusLine;
    }
    
    /**
	 * 是否正确
	 * 
	 * @return
	 */
    public boolean isOK() {
    	return getStatusCode() == 200 ? true : false;
    }

    /**
     * 状态编码
     * 
     * @return
     */
    public final int getStatusCode() {
        return statusCode;
    }

    /**
     * HTTP 版本
     * 
     * @return
     */
    public final String getHttpVersion() {
        return httpVersion;
    }

    /**
     * 返回描述
     * 
     * @return
     */
    public final String getReasonPhrase() {
        return reasonPhrase;
    }

    /**
     * 描述
     * 
     */
    public final String toString() {
    	StringBuffer sb = new StringBuffer();
    	sb.append("{StatusLine = ").append(this.statusLine)
    	  .append(", httpVersion = ").append(this.httpVersion)
    	  .append(", statusCode = ").append(this.statusCode)
    	  .append(", reasonPhrase = ").append(this.reasonPhrase)
    	  .append("}");
    	
        return sb.toString();
    }

    /**
     * 是否以HTTP为起始头
     * 
     * @param s
     * @return
     */
    public static boolean startsWithHTTP(final String s) {
        try {
            int at = 0;
            while (Character.isWhitespace(s.charAt(at))) {
                ++at;
            }
            return ("HTTP".equals(s.substring(at, at + 4)));
        } catch (StringIndexOutOfBoundsException e) {
            return false;
        }
    }
}
